home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / otherobjects / clipboardform.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  18.8 KB  |  474 lines

  1. Public Class ClipboardForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  26.     Friend WithEvents Label1 As System.Windows.Forms.Label
  27.     Friend WithEvents Label2 As System.Windows.Forms.Label
  28.     Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
  29.     Friend WithEvents btnCopyTB As System.Windows.Forms.Button
  30.     Friend WithEvents btnPasteTB As System.Windows.Forms.Button
  31.     Friend WithEvents btnPasteRTB As System.Windows.Forms.Button
  32.     Friend WithEvents btnCopyRTB As System.Windows.Forms.Button
  33.     Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  34.     Friend WithEvents btnCopyPB As System.Windows.Forms.Button
  35.     Friend WithEvents btnPastePB As System.Windows.Forms.Button
  36.     Friend WithEvents btnShowForm As System.Windows.Forms.Button
  37.     Friend WithEvents btnEnumFormats As System.Windows.Forms.Button
  38.     Friend WithEvents btnClearPB As System.Windows.Forms.Button
  39.  
  40.     'Required by the Windows Form Designer
  41.     Private components As System.ComponentModel.Container
  42.  
  43.     'NOTE: The following procedure is required by the Windows Form Designer
  44.     'It can be modified using the Windows Form Designer.  
  45.     'Do not modify it using the code editor.
  46.     Friend WithEvents btnPasteCsv As System.Windows.Forms.Button
  47.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  48.         Me.btnShowForm = New System.Windows.Forms.Button()
  49.         Me.btnPasteRTB = New System.Windows.Forms.Button()
  50.         Me.TextBox1 = New System.Windows.Forms.TextBox()
  51.         Me.btnCopyPB = New System.Windows.Forms.Button()
  52.         Me.btnEnumFormats = New System.Windows.Forms.Button()
  53.         Me.btnCopyTB = New System.Windows.Forms.Button()
  54.         Me.PictureBox1 = New System.Windows.Forms.PictureBox()
  55.         Me.btnCopyRTB = New System.Windows.Forms.Button()
  56.         Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
  57.         Me.btnPasteTB = New System.Windows.Forms.Button()
  58.         Me.btnPastePB = New System.Windows.Forms.Button()
  59.         Me.Label1 = New System.Windows.Forms.Label()
  60.         Me.Label2 = New System.Windows.Forms.Label()
  61.         Me.btnClearPB = New System.Windows.Forms.Button()
  62.         Me.btnPasteCsv = New System.Windows.Forms.Button()
  63.         Me.SuspendLayout()
  64.         '
  65.         'btnShowForm
  66.         '
  67.         Me.btnShowForm.Location = New System.Drawing.Point(24, 336)
  68.         Me.btnShowForm.Name = "btnShowForm"
  69.         Me.btnShowForm.Size = New System.Drawing.Size(312, 32)
  70.         Me.btnShowForm.TabIndex = 5
  71.         Me.btnShowForm.Text = "Show another instance of this form"
  72.         '
  73.         'btnPasteRTB
  74.         '
  75.         Me.btnPasteRTB.Location = New System.Drawing.Point(344, 232)
  76.         Me.btnPasteRTB.Name = "btnPasteRTB"
  77.         Me.btnPasteRTB.Size = New System.Drawing.Size(64, 32)
  78.         Me.btnPasteRTB.TabIndex = 3
  79.         Me.btnPasteRTB.Text = "Paste"
  80.         '
  81.         'TextBox1
  82.         '
  83.         Me.TextBox1.AllowDrop = True
  84.         Me.TextBox1.Location = New System.Drawing.Point(24, 32)
  85.         Me.TextBox1.Multiline = True
  86.         Me.TextBox1.Name = "TextBox1"
  87.         Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
  88.         Me.TextBox1.Size = New System.Drawing.Size(312, 120)
  89.         Me.TextBox1.TabIndex = 0
  90.         Me.TextBox1.Text = ""
  91.         '
  92.         'btnCopyPB
  93.         '
  94.         Me.btnCopyPB.Location = New System.Drawing.Point(624, 32)
  95.         Me.btnCopyPB.Name = "btnCopyPB"
  96.         Me.btnCopyPB.Size = New System.Drawing.Size(64, 32)
  97.         Me.btnCopyPB.TabIndex = 3
  98.         Me.btnCopyPB.Text = "Copy"
  99.         '
  100.         'btnEnumFormats
  101.         '
  102.         Me.btnEnumFormats.Location = New System.Drawing.Point(432, 336)
  103.         Me.btnEnumFormats.Name = "btnEnumFormats"
  104.         Me.btnEnumFormats.Size = New System.Drawing.Size(184, 32)
  105.         Me.btnEnumFormats.TabIndex = 5
  106.         Me.btnEnumFormats.Text = "Display clipboard formats"
  107.         '
  108.         'btnCopyTB
  109.         '
  110.         Me.btnCopyTB.Location = New System.Drawing.Point(344, 32)
  111.         Me.btnCopyTB.Name = "btnCopyTB"
  112.         Me.btnCopyTB.Size = New System.Drawing.Size(64, 32)
  113.         Me.btnCopyTB.TabIndex = 3
  114.         Me.btnCopyTB.Text = "Copy"
  115.         '
  116.         'PictureBox1
  117.         '
  118.         Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  119.         Me.PictureBox1.Location = New System.Drawing.Point(432, 32)
  120.         Me.PictureBox1.Name = "PictureBox1"
  121.         Me.PictureBox1.Size = New System.Drawing.Size(180, 292)
  122.         Me.PictureBox1.TabIndex = 4
  123.         Me.PictureBox1.TabStop = False
  124.         '
  125.         'btnCopyRTB
  126.         '
  127.         Me.btnCopyRTB.Location = New System.Drawing.Point(344, 192)
  128.         Me.btnCopyRTB.Name = "btnCopyRTB"
  129.         Me.btnCopyRTB.Size = New System.Drawing.Size(64, 32)
  130.         Me.btnCopyRTB.TabIndex = 3
  131.         Me.btnCopyRTB.Text = "Copy"
  132.         '
  133.         'RichTextBox1
  134.         '
  135.         Me.RichTextBox1.AllowDrop = True
  136.         Me.RichTextBox1.Location = New System.Drawing.Point(24, 192)
  137.         Me.RichTextBox1.Name = "RichTextBox1"
  138.         Me.RichTextBox1.Size = New System.Drawing.Size(312, 128)
  139.         Me.RichTextBox1.TabIndex = 2
  140.         Me.RichTextBox1.Text = ""
  141.         '
  142.         'btnPasteTB
  143.         '
  144.         Me.btnPasteTB.Location = New System.Drawing.Point(344, 72)
  145.         Me.btnPasteTB.Name = "btnPasteTB"
  146.         Me.btnPasteTB.Size = New System.Drawing.Size(64, 32)
  147.         Me.btnPasteTB.TabIndex = 3
  148.         Me.btnPasteTB.Text = "Paste"
  149.         '
  150.         'btnPastePB
  151.         '
  152.         Me.btnPastePB.Location = New System.Drawing.Point(624, 72)
  153.         Me.btnPastePB.Name = "btnPastePB"
  154.         Me.btnPastePB.Size = New System.Drawing.Size(64, 32)
  155.         Me.btnPastePB.TabIndex = 3
  156.         Me.btnPastePB.Text = "Paste"
  157.         '
  158.         'Label1
  159.         '
  160.         Me.Label1.Location = New System.Drawing.Point(24, 8)
  161.         Me.Label1.Name = "Label1"
  162.         Me.Label1.Size = New System.Drawing.Size(160, 16)
  163.         Me.Label1.TabIndex = 1
  164.         Me.Label1.Text = "A TextBox control"
  165.         '
  166.         'Label2
  167.         '
  168.         Me.Label2.Location = New System.Drawing.Point(24, 168)
  169.         Me.Label2.Name = "Label2"
  170.         Me.Label2.Size = New System.Drawing.Size(160, 16)
  171.         Me.Label2.TabIndex = 1
  172.         Me.Label2.Text = "A RichTextBox control"
  173.         '
  174.         'btnClearPB
  175.         '
  176.         Me.btnClearPB.Location = New System.Drawing.Point(624, 120)
  177.         Me.btnClearPB.Name = "btnClearPB"
  178.         Me.btnClearPB.Size = New System.Drawing.Size(64, 32)
  179.         Me.btnClearPB.TabIndex = 3
  180.         Me.btnClearPB.Text = "Clear"
  181.         '
  182.         'btnPasteCsv
  183.         '
  184.         Me.btnPasteCsv.Location = New System.Drawing.Point(344, 112)
  185.         Me.btnPasteCsv.Name = "btnPasteCsv"
  186.         Me.btnPasteCsv.Size = New System.Drawing.Size(64, 40)
  187.         Me.btnPasteCsv.TabIndex = 7
  188.         Me.btnPasteCsv.Text = "Paste CSV"
  189.         '
  190.         'ClipboardForm
  191.         '
  192.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  193.         Me.ClientSize = New System.Drawing.Size(696, 381)
  194.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnPasteCsv, Me.btnClearPB, Me.btnEnumFormats, Me.btnShowForm, Me.btnCopyPB, Me.btnPastePB, Me.PictureBox1, Me.btnPasteRTB, Me.btnCopyRTB, Me.btnPasteTB, Me.btnCopyTB, Me.RichTextBox1, Me.Label2, Me.Label1, Me.TextBox1})
  195.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  196.         Me.Name = "ClipboardForm"
  197.         Me.Text = "Clipboard demo"
  198.         Me.ResumeLayout(False)
  199.  
  200.     End Sub
  201.  
  202. #End Region
  203.  
  204.     ' show another instance of this form
  205.  
  206.     Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click
  207.         Dim frm As New ClipboardForm()
  208.         frm.Show()
  209.     End Sub
  210.  
  211.     ' copy the contents of the TextBox into the Clipboard 
  212.  
  213.     Private Sub btnCopyTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyTB.Click
  214.         CopyFromTextBox(TextBox1)
  215.     End Sub
  216.  
  217.     ' paste the contents of the Clipboard into the TextBox
  218.  
  219.     Private Sub btnPasteTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPasteTB.Click
  220.         PasteIntoTextBox(TextBox1)
  221.     End Sub
  222.  
  223.     ' copy the contents of the RTB control into the Clipboard 
  224.  
  225.     Private Sub btnCopyRTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyRTB.Click
  226.         CopyFromRichTextBox(RichTextBox1)
  227.     End Sub
  228.  
  229.     ' paste the contents of the Clipboard into the RTB control
  230.  
  231.     Private Sub btnPasteRTB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPasteRTB.Click
  232.         PasteIntoRichTextBox(RichTextBox1)
  233.     End Sub
  234.  
  235.     ' show all clipboard formats
  236.  
  237.     Private Sub btnEnumFormats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnumFormats.Click
  238.         EnumClipboardFormats()
  239.     End Sub
  240.  
  241.     ' copy the contents of the PictureBox into the clipboard
  242.  
  243.     Private Sub btnCopyPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyPB.Click
  244.         If PictureBox1.Image Is Nothing Then
  245.             MessageBox.Show("First paste an image into the PictureBox", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  246.             Exit Sub
  247.         End If
  248.         CopyFromPictureBox(PictureBox1)
  249.     End Sub
  250.  
  251.     ' paste the contents of the clipboard into the PictureBox
  252.  
  253.     Private Sub btnPastePB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPastePB.Click
  254.         PasteIntoPictureBox(PictureBox1)
  255.     End Sub
  256.  
  257.     ' clear the contents of the PictureBox
  258.  
  259.     Private Sub btnClearPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearPB.Click
  260.         PictureBox1.Image = Nothing
  261.     End Sub
  262.  
  263.     ' a reusable routine the copies the contents of a TextBox
  264.     ' into the Clipboard
  265.  
  266.     Sub CopyFromTextBox(ByVal tb As TextBox)
  267.         ' Copy the TextBox's selected text to the clipboard.
  268.         Dim t As String = tb.SelectedText
  269.         ' Copy the entire Text, if no text is selected.
  270.         If t = "" Then t = tb.Text
  271.         ' Proceed only if there is something to be copied.
  272.         If t <> "" Then
  273.             Clipboard.SetDataObject(t)
  274.         End If
  275.     End Sub
  276.  
  277.     ' a reusable routine the pastes the contents of the Clipboard
  278.     ' into a TextBox control
  279.  
  280.     Sub PasteIntoTextBox(ByVal tb As TextBox)
  281.         ' Get the data in the clipboard.
  282.         Dim data As IDataObject = Clipboard.GetDataObject
  283.         ' Check if there is any data in text format, converting it if necessary.
  284.         If data.GetDataPresent(DataFormats.Text, True) Then
  285.             ' If yes, paste into the selection.
  286.             tb.SelectedText = data.GetData(DataFormats.Text, True).ToString
  287.         End If
  288.     End Sub
  289.  
  290.     ' a reusable routine the copies the contents of a RichTextBox
  291.     ' into the Clipboard
  292.  
  293.     Sub CopyFromRichTextBox(ByVal rtf As RichTextBox)
  294.         ' copy the RichTextBox's selected text to the clipboard
  295.         Dim data As New DataObject()
  296.  
  297.         Dim t As String = rtf.SelectedRtf
  298.         ' or the entire Text, if no text is selected
  299.         If t = "" Then t = rtf.Rtf
  300.         ' do the copy only if there is something to be copied
  301.         If t <> "" Then data.SetData(DataFormats.Rtf, t)
  302.  
  303.         ' do it again with the plain text
  304.         t = rtf.SelectedText
  305.         ' Copy the entire Text, if no text is selected.
  306.         If t = "" Then t = rtf.Text
  307.         ' Proceed only if there is something to be copied.
  308.         If t <> "" Then
  309.             data.SetData(DataFormats.Text, t)
  310.         End If
  311.  
  312.         ' Pass True to make the data available to other applications.
  313.         Clipboard.SetDataObject(data, True)
  314.  
  315.     End Sub
  316.  
  317.     ' a reusable routine the pastes the contents of the Clipboard
  318.     ' into a RichTextBox control
  319.  
  320.     Sub PasteIntoRichTextBox(ByVal rtf As RichTextBox)
  321.         ' Get the data in the clipboard.
  322.         Dim data As IDataObject = Clipboard.GetDataObject
  323.  
  324.         ' Check if there is any data in RTF format, 
  325.         ' WITHOUT attempting a conversion
  326.         If data.GetDataPresent(DataFormats.Rtf, False) Then
  327.             ' If available, paste into the RTF selection.
  328.             rtf.SelectedRtf = data.GetData(DataFormats.Rtf).ToString
  329.         ElseIf data.GetDataPresent(DataFormats.Text, True) Then
  330.             ' Else, attempt to get data in plain text format.
  331.             rtf.SelectedText = data.GetData(DataFormats.Text, True).ToString
  332.         End If
  333.     End Sub
  334.  
  335.     ' a  routine the copies the contents of a PictureBox
  336.     ' into the Clipboard
  337.  
  338.     Sub CopyFromPictureBox(ByVal pb As PictureBox)
  339.         Clipboard.SetDataObject(PictureBox1.Image)
  340.     End Sub
  341.  
  342.     ' a reusable routine the pastes the contents of the Clipboard
  343.     ' into a PictureBox control
  344.  
  345.     Sub PasteIntoPictureBox(ByVal pb As PictureBox)
  346.         ' Get the data in the clipboard.
  347.         Dim data As IDataObject = Clipboard.GetDataObject
  348.  
  349.         If data.GetDataPresent(DataFormats.Bitmap) Then
  350.             pb.Image = CType(data.GetData(DataFormats.Bitmap), Image)
  351.         ElseIf data.GetDataPresent(DataFormats.Tiff) Then
  352.             pb.Image = CType(data.GetData(DataFormats.Tiff), Image)
  353.         End If
  354.     End Sub
  355.  
  356.     ' enumerate all Clipboard formats
  357.  
  358.     Sub EnumClipboardFormats()
  359.         Dim t As String
  360.         Dim msg As String
  361.  
  362.         ' Prepare the list of formats currently in the clipboard.
  363.         For Each t In Clipboard.GetDataObject.GetFormats
  364.             msg &= t & ControlChars.CrLf
  365.         Next
  366.         ' Show the list.
  367.         MessageBox.Show(msg, "Current Clipboard formats")
  368.     End Sub
  369.  
  370.     ' this event fires during a D&D operation
  371.  
  372.     Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter, RichTextBox1.DragEnter
  373.         ' Check that the user is dragging a text
  374.         If e.Data.GetDataPresent(DataFormats.Text, True) Then
  375.             If CBool(e.KeyState And 8) Then
  376.                 ' If Ctrl key is pressed, this is a copy operation.
  377.                 e.Effect = e.AllowedEffect And DragDropEffects.Copy
  378.             Else
  379.                 ' Otherwise it is a move operation.
  380.                 e.Effect = e.AllowedEffect And DragDropEffects.Move
  381.             End If
  382.  
  383.         Else
  384.             ' reject any other type
  385.             e.Effect = DragDropEffects.None
  386.         End If
  387.     End Sub
  388.  
  389.     ' this event fires during a D&D operation
  390.  
  391.     Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop, RichTextBox1.DragDrop
  392.         ' Exit if data isn't in text format
  393.         If Not e.Data.GetDataPresent(DataFormats.Text, True) Then Exit Sub
  394.  
  395.         ' Decide whether is a copy or move operation.
  396.         If CBool(e.KeyState And 8) Then
  397.             e.Effect = DragDropEffects.Copy
  398.         Else
  399.             e.Effect = DragDropEffects.Move
  400.         End If
  401.  
  402.         If (TypeOf sender Is RichTextBox) AndAlso e.Data.GetDataPresent(DataFormats.Rtf) Then
  403.             ' If control is a RichTextBox and data is in RTF format, get it.
  404.             Dim rtf As RichTextBox = DirectCast(sender, RichTextBox)
  405.             rtf.SelectedRtf = e.Data.GetData(DataFormats.Rtf).ToString
  406.         Else
  407.             ' Otherwise paste plain text.
  408.             Dim tbase As TextBoxBase = DirectCast(sender, TextBoxBase)
  409.             tbase.SelectedText = e.Data.GetData(DataFormats.Text).ToString
  410.         End If
  411.     End Sub
  412.  
  413.     ' this event can initiate a D&D operation
  414.  
  415.     Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove, RichTextBox1.MouseMove
  416.         ' Exit if no button is pressed.
  417.         If e.Button = 0 Then Exit Sub
  418.  
  419.         ' Get a reference to the control, exit if it contains no text.
  420.         Dim tbase As TextBoxBase = DirectCast(sender, TextBoxBase)
  421.         If tbase.TextLength = 0 Then Exit Sub
  422.         ' Exit if the cursor is inside the control's borders.
  423.         If e.X >= 0 And e.X < tbase.Width And e.Y >= 0 And e.Y < tbase.Height Then Exit Sub
  424.         ' the mouse is being dragged outside the control's client area,
  425.         ' so we can start a drag-and-drop operation.
  426.  
  427.         ' Create a standalone DataObject.
  428.         Dim data As New DataObject()
  429.         ' Store the selected text, or all the text if no selection.
  430.         If tbase.SelectionLength > 0 Then
  431.             data.SetData(DataFormats.Text, tbase.SelectedText)
  432.         Else
  433.             data.SetData(DataFormats.Text, tbase.Text)
  434.         End If
  435.  
  436.         ' If the control is a RichTextBox, store also the selected Rtftext
  437.         ' or its entire contents if no selection.
  438.         If TypeOf sender Is RichTextBox Then
  439.             Dim rtfbox As RichTextBox = DirectCast(sender, RichTextBox)
  440.             If rtfbox.SelectionLength > 0 Then
  441.                 data.SetData(DataFormats.Rtf, rtfbox.SelectedRtf)
  442.             Else
  443.                 data.SetData(DataFormats.Rtf, rtfbox.Rtf)
  444.             End If
  445.         End If
  446.  
  447.         ' Don't accept dropped data while the operation is running.
  448.         Dim saveAllowDrop As Boolean = tbase.AllowDrop
  449.         tbase.AllowDrop = False
  450.  
  451.         ' Start the drag operation - wait until it's completed.
  452.         Dim effect As DragDropEffects = DragDropEffects.Copy Or DragDropEffects.Move
  453.         effect = tbase.DoDragDrop(data, effect)
  454.  
  455.         ' Delete the text if it was a move operation.
  456.         If effect = DragDropEffects.Move Then
  457.             If tbase.SelectionLength > 0 Then
  458.                 tbase.SelectedText = ""
  459.             Else
  460.                 tbase.Text = ""
  461.             End If
  462.         End If
  463.  
  464.         ' Re-enable this control as a D&D target.
  465.         tbase.AllowDrop = saveAllowDrop
  466.     End Sub
  467.  
  468.     ' this event serves to cancel a D&D if the user presses the Esc key
  469.  
  470.     Private Sub TextBox1_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles TextBox1.QueryContinueDrag, RichTextBox1.QueryContinueDrag
  471.         If e.EscapePressed Then e.Action = DragAction.Cancel
  472.     End Sub
  473. End Class
  474.